<xsl:stylesheet version="1.0"
xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx"
                xmlns:edm="http://schemas.microsoft.com/ado/2009/11/edm"
xmlns:ssdl="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"
                exclude-result-prefixes="edmx ssdl edm"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:param name="EntitiesDatabase"></xsl:param>
  <xsl:template match="/edmx:Edmx/edmx:Runtime">
    using System;
    using System.Linq;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    using Rally_MvvmC.Library;
    using System.Data.Entity;
    using Newtonsoft.Json;
    using System.Data.Entity.Infrastructure;
    // Auto Generated File Edmx2ViewModel.xslt

    namespace _24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>
    {
    #region EntityFactory
    public static class EntityFactory
    {
    public static EntityClass GetEntity<xsl:text disable-output-escaping="yes">&lt;</xsl:text>EntityClass<xsl:text disable-output-escaping="yes">&gt;</xsl:text>(string modelName, DbContext dbContext, IEnumerable<xsl:text disable-output-escaping="yes">&lt;</xsl:text>EntityClass<xsl:text disable-output-escaping="yes">&gt;</xsl:text> entitiesCollection, int id, Func<xsl:text disable-output-escaping="yes">&lt;</xsl:text>EntityClass, bool<xsl:text disable-output-escaping="yes">&gt;</xsl:text> predicate, DataCollectionBase dataCollection)
    where EntityClass : class
    {
    string odelName = modelName.Substring(modelName.IndexOf("FROM [dbo].[")+("FROM [dbo].[").Length);
    if (odelName.IndexOf("]") <xsl:text disable-output-escaping="yes">&gt;</xsl:text> -1)
    {
    odelName = odelName.Substring(0, odelName.IndexOf("]"));
    }
    switch (odelName)
    {
    <xsl:for-each select="edmx:ConceptualModels/edm:Schema/edm:EntityType" >
      case "<xsl:value-of select="@Name"/>":
      if (predicate != null)
      { /*return (entitiesCollection as DbSet<xsl:text disable-output-escaping="yes">&lt;</xsl:text>CompanyProfile>).FirstOrDefault(predicate) as EntityClass;*/ }
        else {<xsl:if test="count(edm:Key/edm:PropertyRef) = 1 and count(edm:NavigationProperty[@Name = 'Id']) = 0 and count(edm:Property[@Name = 'Id']) = 0"><xsl:variable name="PrimaryKeyName" select="edm:Key/edm:PropertyRef/@Name" />
      return (entitiesCollection as DbSet<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="@Name"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>).FirstOrDefault(x =<xsl:text disable-output-escaping="yes">&gt;</xsl:text> x.<xsl:value-of select="$PrimaryKeyName"/>.Equals(id)) as EntityClass;
    </xsl:if>} break;
    </xsl:for-each>
    default:
    return null;
    }
    return null;
    }
    }
  #endregion

    public static class ViewModelFactory
    {
    public static ViewModelBase GetViewModel<xsl:text disable-output-escaping="yes">&lt;</xsl:text>ViewModelClass, EntityClass<xsl:text disable-output-escaping="yes">&gt;</xsl:text>(string modelName, DbContext dbContext, IEnumerable<xsl:text disable-output-escaping="yes">&lt;</xsl:text>EntityClass<xsl:text disable-output-escaping="yes">&gt;</xsl:text> entitiesCollection, EntityClass entity, DataCollectionBase dataCollection)
    where ViewModelClass : ViewModelBase<xsl:text disable-output-escaping="yes">&lt;</xsl:text>EntityClass<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    where EntityClass : class
    {
    string odelName = modelName.Substring(modelName.LastIndexOf(".")+1);
    if (odelName.LastIndexOf("_") <xsl:text disable-output-escaping="yes">&gt;</xsl:text> -1)
    {
    odelName = odelName.Substring(0, odelName.LastIndexOf("_"));
    }
    switch (odelName)
    {
    <xsl:for-each select="edmx:ConceptualModels/edm:Schema/edm:EntityType" >
      case "<xsl:value-of select="@Name"/>":
      return new <xsl:value-of select="@Name"/>ViewModel(dbContext, entitiesCollection as DbSet<xsl:text disable-output-escaping="yes">&lt;</xsl:text>_24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>, entity as _24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/>);
    </xsl:for-each>
    default:
    return null;
    }
    }

    }

    <xsl:for-each select="edmx:ConceptualModels/edm:Schema/edm:EntityType" >
      <xsl:variable name="KeyName" select="edm:Key/edm:PropertyRef/@Name" />
      <xsl:variable name="EntityTypeName" select="@Name" />
      public partial class <xsl:value-of select="@Name"/>ViewModel : ViewModelBase<xsl:text disable-output-escaping="yes">&lt;</xsl:text>_24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
      {

      public <xsl:value-of select="@Name"/>ViewModel()
      {
      this.Entity = new <xsl:value-of select="@Name"/>();
      }

      DbContext dbContext = null;
      DbSet<xsl:text disable-output-escaping="yes">&lt;</xsl:text>_24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text> entitiesCollection = null;
      _24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/> entityClass = null;
      public <xsl:value-of select="@Name"/>ViewModel(DbContext dbContext, DbSet<xsl:text disable-output-escaping="yes">&lt;</xsl:text>_24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text> entitiesCollection, _24_Hour_Blogger.Models.<xsl:value-of select="$EntitiesDatabase"/>.<xsl:value-of select="@Name"/> entityClass)
      : base (dbContext, entitiesCollection, entityClass)
      {

      this.dbContext = dbContext;
      this.entitiesCollection = entitiesCollection;
      this.entityClass = entityClass;
      if (this.entityClass == null)
      {
      this.entityClass = new <xsl:value-of select="@Name"/>();
      }
      this.Entity = this.entityClass;
      <xsl:for-each select="edm:NavigationProperty" >
        <xsl:if test="@Name != 'CreatedBy' and @Name != 'UpdatedBy'">
          <!-- Relationship -> (Association)AssociationSet(Name) -> (Name)Association -> EndType(Role) = (ToRole)NavigationProperty -->
          <xsl:variable name="Relationship" select="@Relationship" />
          <xsl:variable name="FromRole" select="@ToRole" />
          <xsl:variable name="AssociationSet" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:AssociationSet[@Association = $Relationship]" />
          <xsl:variable name="Association" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:Association[@Name = $AssociationSet/@Name]" />
          <xsl:variable name="End" select="$Association/edm:End[@Role = $FromRole]" />
          <xsl:variable name="EntitySet" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:EntitySet[@EntityType = $End/@Type]" />
          <xsl:variable name="EntitySetName" select="$EntitySet/@Name" />
          <xsl:variable name="Multiplicity" select="$End/@Multiplicity" />
          <xsl:variable name="EndType" select="substring-after($End/@Type, '.')" />
        </xsl:if>
      </xsl:for-each>

      }
<xsl:if test="count(edm:Key/edm:PropertyRef) = 1 and count(edm:NavigationProperty[@Name = 'Id']) = 0 and count(edm:Property[@Name = 'Id']) = 0">
  <xsl:variable name="PrimaryKeyName" select="edm:Key/edm:PropertyRef/@Name" />
  <xsl:variable name="PrimaryKeyField" select="edm:Property[@Name = $PrimaryKeyName]" />
  // This forces a model where no objects can have a non Int Id field.
  public override Int32 Id {
  get
  {
  return Entity.<xsl:value-of select="edm:Key/edm:PropertyRef/@Name"/>;
  }
  set
  {
  <xsl:if test="$PrimaryKeyField/@Type = 'Int32'">
    Entity.<xsl:value-of select="edm:Key/edm:PropertyRef/@Name"/> = value;
  </xsl:if>
  <xsl:if test="$PrimaryKeyField/@Type = 'Int16'">
    Entity.<xsl:value-of select="edm:Key/edm:PropertyRef/@Name"/> = short.Parse(value.ToString());
  </xsl:if>
  
  }
  }
</xsl:if>

      <xsl:for-each select="edm:Property" >
        <xsl:if test="@Type = 'Binary'">
          public byte[]&#160;<xsl:value-of select="@Name"/>
          { get
          {
          return Entity.<xsl:value-of select="@Name"/>;
          }
          set
          {
          Entity.<xsl:value-of select="@Name"/> = value;
          }
          }
        </xsl:if>
        <xsl:if test="@Type != 'Binary'">
          public <xsl:if test="@Name = 'UpdatedByUserId' or (@Name = 'CreatedByUserId') or @Name = 'UpdatedDate' or (@Name = 'CreatedDate')" >override </xsl:if> <xsl:if test="not(@Nullable) and @Type != 'String'">
            Nullable<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
          </xsl:if><xsl:value-of select="@Type"/><xsl:if test="not(@Nullable) and @Type != 'String'">
            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
          </xsl:if>&#160;<xsl:value-of select="@Name"/>
          { get
          {
          return Entity.<xsl:value-of select="@Name"/>;
          }
          set
          {
          //<xsl:value-of select="@Name"/>--<xsl:value-of select="$KeyName"/>
          <xsl:if test="$KeyName = @Name and ((@Type = 'Int32') or (@Type = 'Int16')) ">
            if (value <xsl:text disable-output-escaping="yes">&gt;</xsl:text> 0)
          </xsl:if>
          if (Entity.<xsl:value-of select="@Name"/> != value)
          {
          Entity.<xsl:value-of select="@Name"/> = value;
          this.IsDirty = true;
          }
          }
          }
        </xsl:if>
      </xsl:for-each>

      <xsl:for-each select="edm:NavigationProperty" >
        <xsl:if test="@Name != 'CreatedBy' and @Name != 'UpdatedBy'">
          <!-- Relationship -> (Association)AssociationSet(Name) -> (Name)Association -> EndType(Role) = (ToRole)NavigationProperty -->
          <xsl:variable name="Relationship" select="@Relationship" />
          <xsl:variable name="ToRole" select="@ToRole" />
          <xsl:variable name="AssociationSet" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:AssociationSet[@Association = $Relationship]" />
          <xsl:variable name="Association" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:Association[@Name = $AssociationSet/@Name]" />
          <xsl:variable name="End" select="$Association/edm:End[@Role = $ToRole]" />
          <xsl:variable name="EntitySet" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:EntitySet[@EntityType = $End/@Type]" />
          <xsl:variable name="EntitySetName" select="$EntitySet/@Name" />
          <xsl:variable name="Multiplicity" select="$End/@Multiplicity" />
          <xsl:variable name="EndType" select="substring-after($End/@Type, '.')" />
          <xsl:if test="$Multiplicity = '*'" >
            private <xsl:if test="@Name = 'UpdatedByUser' or (@Name = 'CreatedByUser')" >override </xsl:if>DataCollectionBase<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="$EndType"/>ViewModel, <xsl:value-of select="$EndType"/>&#160;<xsl:text disable-output-escaping="yes">&gt;</xsl:text>&#160;_<xsl:value-of select="@Name"/>;
            public DataCollectionBase<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="$EndType"/>ViewModel, <xsl:value-of select="$EndType"/>&#160;<xsl:text disable-output-escaping="yes">&gt;</xsl:text>&#160;<xsl:value-of select="@Name"/>
            {
            get
            {
            if (_<xsl:value-of select="@Name"/> == null) {
            if (this.Entity != null)
            _<xsl:value-of select="@Name"/> = new DataCollectionBase<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="$EndType"/>ViewModel, <xsl:value-of select="$EndType"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>(dbContext, DataStore._24_Hour_Blogger<xsl:value-of select="$EntitiesDatabase"/>Entities.<xsl:value-of select="$EntitySetName"/>, this.Entity.<xsl:value-of select="@Name"/>);
            else
            _<xsl:value-of select="@Name"/> = null;
            }

            return _<xsl:value-of select="@Name"/>;
            }
            set{
            _<xsl:value-of select="@Name"/> = value;
            }
            }
          </xsl:if>
          <xsl:if test="$Multiplicity != '*'" >
private <xsl:if test="@Name = 'UpdatedByUser' or (@Name = 'CreatedByUser')" >override </xsl:if><xsl:value-of select="$EndType"/>ViewModel _<xsl:value-of select="@Name"/>;
            public <xsl:if test="@Name = 'UpdatedByUser' or (@Name = 'CreatedByUser')" >override </xsl:if><xsl:value-of select="$EndType"/>ViewModel&#160;<xsl:value-of select="@Name"/>
            { get
            {

            if (_<xsl:value-of select="@Name"/> == null) {
            
            if (this.Entity != null <xsl:text disable-output-escaping="yes">&amp;&amp;</xsl:text> this.Entity.<xsl:value-of select="@Name"/> != null)
            _<xsl:value-of select="@Name"/> = new <xsl:value-of select="$EndType"/>ViewModel(dbContext, DataStore._24_Hour_Blogger<xsl:value-of select="$EntitiesDatabase"/>Entities.<xsl:value-of select="$EntitySetName"/>, this.Entity.<xsl:value-of select="@Name"/>);
            else
            if (dbContext == null)
            _<xsl:value-of select="@Name"/> = new <xsl:value-of select="$EndType"/>ViewModel();
            else
            _<xsl:value-of select="@Name"/> = new <xsl:value-of select="$EndType"/>ViewModel(dbContext, DataStore._24_Hour_Blogger<xsl:value-of select="$EntitiesDatabase"/>Entities.<xsl:value-of select="$EntitySetName"/>, null);
            }
            
            return _<xsl:value-of select="@Name"/>;


            }
            set{
            if (this.dbContext != null)
            if (this.Entity.<xsl:value-of select="@Name"/> != value.Entity)
            {
            this.Entity.<xsl:value-of select="@Name"/> = value.Entity;
            }
            if (_<xsl:value-of select="@Name"/> != value)
            {
            _<xsl:value-of select="@Name"/> = value;
            }
            }
            }
          </xsl:if>

        </xsl:if>
      </xsl:for-each>
      }
    </xsl:for-each>


#region GetModifiedViewModels
    <xsl:for-each select="edmx:ConceptualModels/edm:Schema/edm:EntityType" >
      <xsl:variable name="KeyName" select="edm:Key/edm:PropertyRef/@Name" />
      <xsl:variable name="EntityTypeName" select="@Name" />
      public partial class <xsl:value-of select="@Name"/>ViewModel 
      {
      public override Collection<xsl:text disable-output-escaping="yes">&lt;</xsl:text>IViewModel<xsl:text disable-output-escaping="yes">&gt;</xsl:text> GetModifiedViewModels()
      {
        var results = base.GetModifiedViewModels();
        <xsl:for-each select="edm:NavigationProperty" >
        <!-- Relationship -> (Association)AssociationSet(Name) -> (Name)Association -> EndType(Role) = (ToRole)NavigationProperty -->
        <xsl:variable name="Relationship" select="@Relationship" />
        <xsl:variable name="ToRole" select="@ToRole" />
        <xsl:variable name="AssociationSet" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:AssociationSet[@Association = $Relationship]" />
        <xsl:variable name="Association" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:Association[@Name = $AssociationSet/@Name]" />
        <xsl:variable name="End" select="$Association/edm:End[@Role = $ToRole]" />
        <xsl:variable name="EntitySet" select="/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:EntitySet[@EntityType = $End/@Type]" />
        <xsl:variable name="EntitySetName" select="$EntitySet/@Name" />
        <xsl:variable name="Multiplicity" select="$End/@Multiplicity" />
        <xsl:variable name="EndType" select="substring-after($End/@Type, '.')" />
        <xsl:if test="$Multiplicity = '*'" >
          if (_<xsl:value-of select="@Name"/> != null) {
          foreach (var result in _<xsl:value-of select="@Name"/>)
          {
          if (result.IsDirty)
          {
            results.Add(result);
          }
          }
          }
        </xsl:if>
          
        </xsl:for-each>
        return results;
        
        }
        }
      </xsl:for-each>
#endregion

    }
  </xsl:template>

</xsl:stylesheet>
